package com.example.activity;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.example.Base.BasePersonActivity;
import com.example.administrator.olddoctor.R;
import com.example.bean.SeekHistoryEntity;
import com.example.db.SimpleEntityTemplate;
import com.example.tools.StringUtils;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
/**
 *  搜索界面
 */
public class SeekActivity extends BasePersonActivity implements View.OnClickListener,TextWatcher{

    private EditText edtext_seek;
    private ListView listview_seek;
    private List<SeekHistoryEntity> historyDate;
    private SimpleEntityTemplate<SeekHistoryEntity> historyTemplate;
    private MyAdapter myadapter;
    private LinearLayout footview;
    private Boolean isRefresh = false;
    private Boolean isNull = true;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_seek);
        setActionBar(getResources().getDrawable(R.mipmap.call_back), null,"搜索",null,null,null);
        init();
        initDate();
    }

    private void init() {
        edtext_seek = (EditText) findViewById(R.id.edtext_seek);
        listview_seek = (ListView) findViewById(R.id.listview_seek);
        historyDate = new ArrayList<>();
        historyTemplate = new SimpleEntityTemplate<>(this,SeekHistoryEntity.class);
    }

    private void initDate() {
        select();
        if(isRefresh){
            myadapter.setDate(historyDate);
            myadapter.notifyDataSetChanged();
        } else {
            myadapter = new MyAdapter();
            myadapter.setDate(historyDate);
            listview_seek.setAdapter(myadapter);
        }
        if(isNull){
            footview = (LinearLayout) LayoutInflater.from(SeekActivity.this).inflate(R.layout.seek_footview,null,false);
            footview.setOnClickListener(this);
            listview_seek.addFooterView(footview);
        }
    }

    public void select(){
        try {
            historyDate =  historyTemplate.selectList();
            if(historyDate.size()==0){
                isNull = false;
            } else {
                isNull = true;
            }
        } catch (SQLException e) {
            Log.e(getClass().getName(),"数据库查询错误",e);
            e.printStackTrace();
        }
    }

    public void insert(View view){
        String text = edtext_seek.getText().toString();
        if(StringUtils.isEmpty(text)){
            showToast("您不能输入空的文字");
           return;
        } else {
            SeekHistoryEntity seekhistory = new SeekHistoryEntity();
            seekhistory.setName(text);
            try {
                historyTemplate.insert(seekhistory);
            } catch (SQLException e) {
                Log.e(getClass().getName(),"数据库插入错误",e);
                e.printStackTrace();
            }
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        if(isRefresh){
            initDate();
        }
    }

    @Override
    protected void onStop() {
        super.onStop();
        isRefresh = true;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.seek_footLinearLayout:
                 if(delete()!=100000){
                  historyDate.clear();
                  myadapter.setDate(historyDate);
                  myadapter.notifyDataSetChanged();
                  listview_seek.removeFooterView(footview);
                  showToast("删除成功");
                 }
                break;
        }
    }

    public int delete(){
        try {
            return historyTemplate.truncate();
        } catch (SQLException e) {
            Log.e(getClass().getName(),"数据库删除错误",e);
            e.printStackTrace();
        }
        return 100000;
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {

    }

    @Override
    public void afterTextChanged(Editable s) {

    }

    class MyAdapter extends BaseAdapter{

        private List<SeekHistoryEntity> historyDate;

        public void setDate(List<SeekHistoryEntity> historyDate){
            this.historyDate = historyDate;
        }

        @Override
        public int getCount() {
            return historyDate.size();
        }

        @Override
        public Object getItem(int position) {
            return historyDate.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {

            MyHolder myholder;
            if(null == convertView){
                myholder = new MyHolder();
                convertView = LayoutInflater.from(SeekActivity.this).inflate(R.layout.item_seek_list,parent,false);
                myholder.historyName = (TextView) convertView.findViewById(R.id.text_seek_history);
                convertView.setTag(myholder);
            } else {
                myholder = (MyHolder) convertView.getTag();
            }
            myholder.historyName.setText(historyDate.get(position).getName());
            return convertView;
        }
        class MyHolder {
            TextView historyName;
        }
    }
}
